﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Script_Text_Editor_WPF_Edition.Library
{
    #region Interface

    /// <summary>
    /// 实现一个包含各种类型的库的接口
    /// </summary>
    public interface ILibrary
    {
        string GetLibraryInfo();

        string GetSupportExtensions();

        string GetStringBreakLineChar();

        bool GetCanMakeNewLine();

        string GetNewLineBreakChar();

        string GetStringCollection(byte[] file);

        byte[] SetStringCollection(string collection);
    }

    #endregion Interface

    class LibraryLinker
    {
        /// <summary>
        /// 库接口
        /// </summary>
        ILibrary library;

        /// <summary>
        /// 构造函数
        /// </summary>
        public LibraryLinker()
        {
            int type = Properties.Settings.Default.LibraryType;
            string path = Properties.Settings.Default.LibraryPath;

            //检查库是否存在
            if ((!File.Exists(path)) && (type != 0))
            {
                System.Windows.Forms.MessageBox.Show("指定的解析库不存在，使用纯文本模式。");
                type = 0;
            }

            try
            {
                //初始化初始化库
                switch (type)
                {
                    case 1://DynamicCodeWrapper
                        library = new DynamicCodeWrapper(path, Encoding.UTF8);
                        break;
                    case 2://NetWrapper
                        library = new NETWrapper(path);
                        break;
                    //case 3://Win32Wrapper
                    //    library = new Win32Wrapper(path);
                    //    break;
                    default:
                        library = new PureTextWrapper();
                        break;
                }
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                Environment.Exit(0);
            }
        }

        public string GetLibraryInfo()
        {
            return library.GetLibraryInfo();
        }

        public string GetSupportExtensions()
        {
            return library.GetSupportExtensions();
        }

        public string GetStringBreakLineChar()
        {
            return library.GetStringBreakLineChar();
        }

        public bool GetCanMakeNewLine()
        {
            return library.GetCanMakeNewLine();
        }

        public string GetNewLineBreakChar()
        {
            return library.GetNewLineBreakChar();
        }

        public string GetStringCollection(byte[] file)
        {
            return library.GetStringCollection(file);
        }

        public byte[] SetStringCollection(string file)
        {
            return library.SetStringCollection(file);
        }
    }
}